﻿//-----------------------------------------------------------------------
// <copyright>
// This file is subject to the terms and conditions of the Microsoft Public License (MS-PL).
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL for more details.
// </copyright>
//----------------------------------------------------------------------- 
using System;
using System.Windows;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Fulcrum.UnitTests.WeakEventManager
{
    [TestClass]
    public class FulcrumTestEventSourceWeakEventManagerTests
    {
        [TestMethod]
        public void AddListenerTest()
        {
            var listener = new Listener();
            var eventSource = new FulcrumTestEventSource();

            FulcrumTestEventSourceWeakEventManager.AddListener(eventSource, listener);

            eventSource.RaiseTestEvent();

            Assert.AreEqual(typeof(FulcrumTestEventSourceWeakEventManager), listener.ManagerType);
            Assert.AreSame(eventSource, listener.Sender);
            Assert.AreSame(EventArgs.Empty, listener.EventArgs);
        }

        [TestMethod]
        public void RemoveListenerTest()
        {
            var listener = new Listener();
            var eventSource = new FulcrumTestEventSource();

            FulcrumTestEventSourceWeakEventManager.AddListener(eventSource, listener);
            FulcrumTestEventSourceWeakEventManager.RemoveListener(eventSource, listener);

            eventSource.RaiseTestEvent();

            Assert.IsNull(listener.ManagerType);
        }

        class Listener : IWeakEventListener
        {
            public Listener()
            {
                // nothing
            }

            public bool ReceiveWeakEvent(Type managerType, object sender, EventArgs e)
            {
                Assert.IsNotNull(managerType);
                Assert.IsNull(this.ManagerType);

                this.ManagerType = managerType;
                this.Sender = sender;
                this.EventArgs = e;

                return true;
            }

            public Type ManagerType
            {
                get;
                private set;
            }

            public object Sender
            {
                get;
                private set;
            }

            public EventArgs EventArgs
            {
                get;
                private set;
            }
        }
    }
}
